<!DOCTYPE html>
<html xmlns:sec="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8">
    <title>角色管理</title>
    <link href="/static/pengpai/component/pear/css/pear.css" rel="stylesheet" />
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">角色名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="roleName" placeholder="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="role-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="role-table" lay-filter="role-table"></table>
    </div>
</div>

<script type="text/html" id="role-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add" sec:authorize="hasAuthority('sys:role:create')">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove" sec:authorize="hasAuthority('sys:role:delete')">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </button>
</script>

<script type="text/html" id="role-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit" sec:authorize="hasAuthority('sys:role:update')"><i class="layui-icon layui-icon-edit">编辑</i></button>
    <button class="pear-btn pear-btn-warming pear-btn-sm" lay-event="power" sec:authorize="hasAuthority('sys:role:grant')"><i class="layui-icon layui-icon-vercode">授权</i></button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove" sec:authorize="hasAuthority('sys:role:delete')"><i class="layui-icon layui-icon-delete">删除</i></button>
</script>

<script type="text/html" id="role-enable">
    <input type="checkbox" name="enable" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="role-enable"  {{ d.enable== true ? 'checked' : '' }} />
</script>

<script src="/static/pengpai/component/layui/layui.js"></script>
<script src="/static/pengpai/component/pear/pear.js"></script>
<script>
    layui.use(['table','form','jquery'],function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;

        let cols = [
            [
                {type:'checkbox'},
                {title: '角色名', field: 'roleName', align:'center'},
                {title: '角色编码', field: 'roleKey', align:'center'},
                {field: 'createUser', title: '创建者', align:'center'},
                {field: 'createTime', title: '创建时间', align:'center'},
                {field: 'updateUser', title: '更新者', align:'center'},
                {field: 'updateTime', title: '更新时间', align:'center'},
                {title: '操作', toolbar: '#role-bar', align:'center', width:300},
            ]
        ]
        table.render({
            elem: '#role-table',
            url: '/sysRole/list',
            page: true ,
            cols: cols ,
            skin: 'line',
            toolbar: '#role-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
        });
        table.on('tool(role-table)', function(obj){
            if(obj.event === 'remove'){
                window.remove(obj);
            } else if(obj.event === 'edit'){
                window.edit(obj);
            } else if(obj.event === 'power'){
                window.power(obj);
            }
        });

        table.on('toolbar(role-table)', function(obj){
            if(obj.event === 'add'){
                window.add();
            } else if(obj.event === 'refresh'){
                window.refresh();
            } else if(obj.event === 'batchRemove'){
                window.batchRemove(obj);
            }
        });

        form.on('submit(role-query)', function(data){
            console.log(data)
            table.reload('role-table', {
                page: {
                    curr: 1
                }
                , where: {
                    key: data.field.roleName
                }
            }, 'data');

            return false;

        });

        form.on('switch(role-enable)', function(obj){
            layer.tips(this.value + ' ' + this.name + '：'+ obj.elem.checked, obj.othis);
        });

        window.add = function(){
            layer.full(layer.open({
                type: 2,
                title: '新增',
                shade: 0.1,
                area: ['500px', '400px'],
                content: 'add'
            }))
        }

        window.power = function(obj){
            layer.full(layer.open({
                type: 2,
                title: '授权',
                shade: 0.1,
                area: ['320px', '400px'],
                content: 'grant?id='+obj.data.roleId
            }));

        }

        window.edit = function(obj){
            layer.full(layer.open({
                type: 2,
                title: '修改',
                shade: 0.1,
                area: ['500px', '400px'],
                content: 'edit?id='+obj.data.roleId
            }))
        }

        window.remove = function(obj){
            layer.confirm('确定要删除该角色', {icon: 3, title:'提示'}, function(index){
                layer.close(index);
                let ids = [];
                ids.push(obj.data['roleId']);
                var idstr = ids.toString();
                $.post('/sysRole/del',{'ids':idstr},function (r){
                    console.info(r);
                    table.reload('role-table', {
                        done: function (res, curr, count) {
                            if (curr > 1 && res.data.length === 0) {
                                curr = curr - 1;
                                table.reload('role-table', {
                                    page: {
                                        curr: curr
                                    },
                                })
                            }
                        }
                    });
                });
            });
        }

        window.batchRemove = function(obj){
            let data = table.checkStatus(obj.config.id).data;
            if(data.length === 0){
                layer.msg("未选中数据",{icon:3,time:1000});
                return false;
            }
            let ids = [];
            for(let i = 0;i<data.length;i++){
                ids.push(data[i].roleId);
            }
            var idstr = ids.toString();
            layer.confirm('确定要删除这些用户', {icon: 3, title:'提示'}, function(index){
                layer.close(index);
                $.post('/sysRole/del',{'ids':idstr},function (r){
                    console.info(r);
                    table.reload('role-table', {
                        done: function (res, curr, count) {
                            if (curr > 1 && res.data.length === 0) {
                                curr = curr - 1;
                                table.reload('role-table', {
                                    page: {
                                        curr: curr
                                    },
                                })
                            }
                        }
                    });
                });

            });
        }
        window.refresh = function(){
            table.reload('role-table');
        }
    })
</script>
</body>
</html>
